#!/usr/bin/env python
#This is the main program. It should be *very* simple.
import subprocess, atexit
import commands, os


#the following lines establish a link with the camera via the USB port, these run 
#automatically when sx_main is excecuted
try: 
        if not os.path.exists('/tmp/fiberfeed_fifo'):
                dummy=subprocess.call('mkfifo /tmp/fiberfeed_fifo', shell=True)
        if not ('indiserver' and '/tmp/fiberfeed_fifo') in os.popen("ps aux").read():
                indiserver_process=subprocess.Popen(['indiserver','-f','/tmp/fiberfeed_fifo','-p','7779'],stdin=subprocess.PIPE, stdout=subprocess.PIPE)
                #indiserver_process=subprocess.Popen('indiserver -f /tmp/myFIFO -p 7779',shell=True)
                sxserver_process=subprocess.call('echo start indi_v4l2_ccd -n \"V4L2 CCD\" > /tmp/fiberfeed_fifo', shell=True)
                #procs=[indiserver_process]
except Exception: print 'Unable to start indi server for fiberfeed'





import fiberfeed_server
import sys
sys.path.append('../common')
import server_socket
import find_port
f = find_port.FindPort()
Port = f.findPort('fiberfeed')
ff=fiberfeed_server.FiberFeedServer()
ss=server_socket.ServerSocket(Port, 'fiberfeed', ff)	
ss.add_job(ff.imaging_loop)
ss.run()
fiberfeed_server.indi.quit()
ss.close()
